﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace DAL
{
    public class PowerDAL
    {
        SqlConnection str = new SqlConnection("Data Source=PC201609040948;Initial Catalog=TianTianDai_Db;Persist Security Info=True;User ID=sa;pwd=123456");
        /// <summary>
        /// 权限表的显示
        /// </summary>
        /// <returns></returns>
        public List<Powers>  Power_Show()
        {
            str.Open();
            string sql = "select * from Powers";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            str.Close();
            List<Powers> list = new List<Powers>();
            foreach (DataRow item in ta.Rows)
            {
                Powers j = new Powers();
                j.PowerId = Convert.ToInt32(item["PowerId"]);
                j.PowerName = item["PowerName"].ToString();
                j.PowerGroupId = Convert.ToInt32(item["PowerGroupId"]);
                
                list.Add(j);
            }
            return list;

        }
        /// <summary>
        /// 删除权限表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Powers_Delete(int id)
        {
            str.Open();
            string sql = "delete Powers where PowerId='" + id + "'";
            SqlCommand com = new SqlCommand(sql, str);
            var i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 添加权限表信息
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int Power_Add(Powers p)
        {
            str.Open();
            string sql = "insert into Powers values('" + p.PowerGroupId + "','" + p.PowerName + "')";
            SqlCommand com = new SqlCommand(sql, str);
            var i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 显示权限表详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Powers Power_Content(int id)
        {
            str.Open();
            string sql = "select * from Powers where PowerId='" + id + "'";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            str.Close();
            List<Powers> list = new List<Powers>();
            foreach (DataRow item in ta.Rows)
            {
                Powers j = new Powers();
                j.PowerId = Convert.ToInt32(item["PowerId"]);
                j.PowerName = item["PowerName"].ToString();
                j.PowerGroupId = Convert.ToInt32(item["PowerGroupId"]);

                list.Add(j);
            }
            return list.FirstOrDefault();
        }
        /// <summary>
        /// 修改权限表信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public int Power_Edit(Powers data)
        {
            str.Open();
            string sql = "update Powers set  PowerName='" + data.PowerName + "',PowerGroupId='" + data.PowerGroupId + "' where PowerId='" + data.PowerId + "'";
            SqlCommand com = new SqlCommand(sql, str);
            int i = com.ExecuteNonQuery();
            return i;
        }
     
    }
}
